package com.study.mybatis.test;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class JdkProxy implements Fun{
    public static void main(String[] args) {
        Fun test2 = (Fun) new FunProxy().bind(new JdkProxy());
        test2.say();
    }

    @Override
    public void say() {
        System.out.println("say");
    }
}

interface Fun {
    void say();
}

class FunProxy implements InvocationHandler {
    Object obj = null;
    public Object bind(Object object) {
        obj = object;
        return Proxy.newProxyInstance(object.getClass().getClassLoader(), object.getClass().getInterfaces(), this);
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("pre_say");
        Object result = method.invoke(obj, args);
        System.out.println("sub_say");
        return result;
    }
}